<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-wait-row3-widget" :style="getStyle('head')">
      <div class="widget-wrap">
        <div class="queue-view">
          <template v-for="(item,key) in waitingPatientInfos">
            <div class="child-item" :key="key">
              <div class="queue-name item">
                <ScrollX>
                  {{ item[optionsInfo['patientRoomNameKey'] || 'treatmentRoomName'] || '\u00a0' }}
                </ScrollX>
              </div>
              <div class="patient-name item">
                <ScrollX>{{ getCurrentPatient(item) }}</ScrollX>
              </div>
            </div>
          </template>
        </div>
        <div class="register-view">
          <template v-for="(item,key) in waitingPatientInfos">
            <div class="child-item" :key="key" :ref="`register-${widgetKey}-${key}`">
              <div class="register-name item" :style="getStyle('registerName')">
                <ScrollX> {{ item[optionsInfo.registerNameKey] || (item.registerObjName) || '\u00a0' }}</ScrollX>
              </div>
              <div class="patient-info item" :key="`register-${widgetKey}-${key}`">
                <vue-seamless-scroll
                    class="scroll"
                    :data="(item.waitPatientList)"
                    :hoverStop="false"
                    @ScrollEnd="handleScrollEnd"
                    :class-option="classOptionPatient(item?.waitPatientList || [])"
                    :ref="`scroll-${item.workerId}`"
                >
                  <div class="inside-content">
                    <template v-if="!!(item?.waitPatientList?.length)">
                      <template v-for="(patientItem) in getWaitPatient(item)">
                        <div v-if="true" class="wait-patient-inside" v-html="getWaitPatientContent(patientItem)"
                             :style="getStyle('waitPatient',`register-${key}`)">
                          {{ patientItem?.orderNo || '\u00a0' }} {{ patientItem?.patientName || '\u00a0' }}
                        </div>
                      </template>
                    </template>
                    <template v-else>
                      <div class="wait-patient-inside-empty"/>
                    </template>
                  </div>
                </vue-seamless-scroll>
              </div>
            </div>
          </template>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from 'uuid';
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from 'vue-seamless-scroll';
import {buildParentApi} from "@/utils/designer/util";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

let buildParent = buildParentApi();
export default {
  name: "scene-waiting-row3-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
  },

  components: {
    ScrollX,
    CompOperate,
    vueSeamlessScroll
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
    }
  },

  computed: {
    // 患者提示的滚动
    classOptionPatient() {
      return function (row) {
        let {openScroll, waitingNumber, waitingVisitStep, viewNumber} = this.optionsInfo;
        return {
          step: [1, 1.5, 2, 2.5, 3][waitingVisitStep - 1], // 数值越大速度滚动越快
          hoverStop: true,
          direction: 1, // 0向下 1向上 2向左 3向右
          limitMoveNum: (openScroll && (row?.length >= viewNumber)) ? waitingNumber : 99999, // 开始无缝滚动的数据量 this.dataList.length
        };
      }
    },

    // 设置的各元素的行内样式
    getStyle() {
      return function (val, el) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            return Obj;
          case "registerName":
            Reflect.set(Obj, "color", (info.headerFontColor));
            Reflect.set(Obj, "backgroundColor", (info.headerBackgroundColor));
            return Obj;
          case "waitPatient":
            let height = (info.contentHeight - 180 - 60 - 98)
            Reflect.set(Obj, "height", pxToVw(height / info.viewNumber));
            Reflect.set(Obj, "lineHeight", pxToVw(height / info.viewNumber));
            Reflect.set(Obj, "fontSize", pxToVw(info.patientFontSize));
            return Obj;
        }
      };
    },

    // 列表的队列的患者
    waitingPatientInfos() {
      let listInfo;
      let devType = (this.devType);
      let {showRowNums} = (this.optionsInfo);
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      if (devType) {
        listInfo = (WaitingPatientList.apiData)
      } else {
        listInfo = (WaitingPatientList.mockData)
      }
      if (listInfo?.length > showRowNums) {
        return listInfo.slice(0, showRowNums);
      } else {
        let arr = (new Array(showRowNums - listInfo?.length).fill({}));
        return [...listInfo, ...arr];
      }
    },

    // 当前就诊的患者|| 医生暂停接诊
    getCurrentPatient() {
      return function (val) {
        /**
         * 在线状态 0在线 1暂停 2退出
         **/
        const self = this;
        let type = parseInt(val.onlineStatus);
        switch (type) {
          case 0:
          case 2:
            if (!!(val?.treatingPatient?.length)) {
              let currentPatient = Array();
              (val.treatingPatient).forEach((item) => {
                let orderNo = item?.orderNo;
                let patientOrderNoInfo = '';
                if (!!orderNo) {
                  patientOrderNoInfo = `${(item.orderNo) || '\u00a0'}`;
                }
                let temp = `${patientOrderNoInfo} ${(item.patientName) || '\u00a0'}`
                currentPatient.push(temp)
              })
              return (self.$lodash.join(currentPatient, ' '));
            } else {
              return '\u00a0'
            }
          case 1:
            return '暂停接诊';
        }
      }
    },

    // 列表候诊患者
    getWaitPatient() {
      return function (row) {
        let {viewNumber = 12} = this.optionsInfo
        if (row?.waitPatientList?.length) {
          let len = row?.waitPatientList?.length;
          if (len >= viewNumber) {
            return row.waitPatientList
          } else {
            let temp = Array(viewNumber - len).fill({});
            return [...row.waitPatientList, ...temp]
          }
        } else {
          return Array(viewNumber).fill({});
        }
      }
    },

    //候诊患者信息
    getWaitPatientContent() {
      return function (row) {
        let orderNoInfo;
        let {orderTag} = this.optionsInfo;
        if (orderTag && (row?.orderTagColor)) {
          orderNoInfo = (row.orderNo) ?
              `<span style='color:${row?.orderTagColor || 'transparent'}'>${row?.orderNo || '\u00a0'}</span>` : ''
        } else {
          orderNoInfo = `${row?.orderNo || '\u00a0'}`
        }
        if (!!this.$lodash.trim(orderNoInfo)) {
          return `${orderNoInfo} ${row?.patientName || '\u00a0'}`
        } else {
          return `${row?.patientName || '\u00a0'}`
        }
      }
    },
  },

  methods: {
    // 等待候诊滚动结束
    handleScrollEnd() {
      console.log('等待就诊滚动结束');
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          let patientList = this.$lodash.cloneDeep(data);
          this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>

.scene-wait-row3-widget {
  background-color: transparent;
  display: flex;
  flex-direction: row;
  margin: 0 28px 0 28px;
  overflow: hidden;
  box-sizing: border-box;

  .widget-wrap {
    background-color: transparent;
    color: #333;
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    overflow: hidden;
    box-sizing: border-box;
    padding: 20px 0;

    //顶部队列
    .queue-view {
      height: 180px;
      width: 100%;
      flex-shrink: 0;
      overflow: hidden;
      display: flex;
      justify-content: space-between;
      flex-direction: row;

      .child-item {
        display: flex;
        flex-direction: column;
        flex-basis: 0;
        flex-grow: 1;
        margin-right: 12px;
        margin-left: 12px;
        background: rgba(0, 71, 143, 1);
        border-radius: 16px;
        overflow: hidden;
        color: #FFFFFF;
        font-size: 50px;
        flex-shrink: 0;

        &:last-child {
          margin-right: 0;
        }

        &:first-child {
          margin-left: 0;
        }


        .item {
          display: flex;
          width: 100%;
          justify-content: center;
          align-self: center;
          align-items: center;
        }

        .queue-name {
          height: 90px;
        }

        .patient-name {
          font-size: 54px;
          height: calc(100% - 90px);
          background: rgba(4, 88, 179, 1);
        }
      }
    }

    //底部号别
    .register-view {
      display: flex;
      flex-direction: row;
      height: calc(100% - 180px - 22px);
      margin-top: 22px;
      flex-shrink: 0;
      font-size: 50px;
      overflow: hidden;

      box-sizing: border-box;

      .child-item {
        margin-right: 12px;
        margin-left: 12px;
        background: white;
        flex-basis: 0;
        flex-grow: 1;
        border-radius: 16px;
        overflow: hidden;

        &:first-child {
          margin-left: 0;
        }

        &:last-child {
          margin-right: 0;
        }

        .register-name {
          width: 100%;
          height: 98px;
          color: #E8E8E8;
          background: #0059B3;
        }

        .patient-info {
          height: calc(100% - 98px);
          overflow: hidden;
          .inside-content {
            display: flex;
            flex-direction: column;
            width: 100%;
            align-items: center;
            align-self: center;
            align-content: center;
            justify-content: center; /* 如果你也想水平居中 */

            .wait-patient-inside {
              color: #333333;
              font-size: 46px;
              background: rgba(255, 255, 255, 1);
              @include singleEllipsis(100%);
              text-align: center;
              padding: 0 14px;
              box-sizing: border-box;
              width: 100%;
              overflow: hidden;
              border-bottom: 1px #dcdee2 solid;
            }
          }

          .wait-patient-inside-empty {
            min-height: 480px;
            margin-top: 10px;
            background: white;
          }
        }
      }
    }
  }
}
</style>